package com.vframework.system.permission.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vframework.base.controller.BaseController;
import com.vframework.common.response.R;
import com.vframework.config.annotation.admin.AdminRestController;
import com.vframework.system.permission.entity.SysPermission;
import com.vframework.system.permission.service.SysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.async.WebAsyncTask;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author codeGenerator
 * @since 2022-10-28
 */
@AdminRestController
@RequestMapping("/permission/sys-permission")
@Api(tags = "权限管理")
public class SysPermissionController extends BaseController {
    @Autowired
    private SysPermissionService permissionService;

    @GetMapping
    @PreAuthorize("hasAuthority('sys:permission:list')")
    @ApiOperation(value = "权限列表")
    public WebAsyncTask<R<List<SysPermission>>> list() {
        WebAsyncTask<R<List<SysPermission>>> webAsyncTask = new WebAsyncTask<>(5000, () -> R.success(permissionService.list(new QueryWrapper<>())));
        webAsyncTask.onTimeout(() -> {
            System.out.println("调用超时");
            return null;
        });
        return webAsyncTask;
    }

    @GetMapping("/sync")
    @PreAuthorize("hasAuthority('sys:permission:sync')")
    @ApiOperation(value = "权限同步")
    public R sync() {
        try {
            permissionService.sync();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return R.success();
    }
}
